import 'package:cashbook/custom/DatumLegendWithMeasures.dart';
import 'package:cashbook/custom/SimpleBarChart.dart';
import 'package:cashbook/custom/CPieChart.dart';
import 'package:cashbook/model/Accounts.dart';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart';

class ChartPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ChartPageState();
  }
}

class ChartPageState extends State<ChartPage> {
  List<OrdinalSales> data = List();
  List<OrdinalSales> data_sr = List();
  List<LinearSales> data_sz = []; //收支

  @override
  void initState() {
    getLocalData();
  }

  void getLocalData() {
    var data = DateTime.now();
    var year = data.year;
    var month = data.month;
    getSZData(year);
    getLocalDataByYearAndMonth(year, month);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('报表'),
        leading: Text(''),
        centerTitle: true,
      ),
      body: Center(
        child: ListView(
          children: <Widget>[
            CPieChart(),
            Container(
              margin: EdgeInsets.only(top: 10),
              child: Text('本年度各月支出统计(单位：元)'),
            ),
            Container(
              width: double.infinity,
              height: 200,
              child: SimpleBarChart(data),
            ),
            Divider(
              color: Colors.grey,
            ),
            Container(
              margin: EdgeInsets.only(top: 10),
              child: Text('本年度各月收入统计(单位：元)'),
            ),
            Container(
              width: double.infinity,
              height: 200,
              child: SimpleBarChart(
                data_sr,
                color: Colors.red,
              ),
            ),
            Divider(
              color: Colors.grey,
            ),
            Container(
              margin: EdgeInsets.only(top: 10),
              child: Text('本年收支占比'),
            ),
            Container(
              height: 200,
              width: double.infinity,
              child: DatumLegendWithMeasures(data_sz),
            )

//            Container(
//              width: double.infinity,
//              height: 200,
//              child: SimpleSeriesLegend(_simpleSeriesLegendData),
//            )
          ],
        ),
      ),
    );
  }

  void getLocalDataByYearAndMonth(int year, int month) async {
    List<OrdinalSales> list =
        await AccountsProvider().findByYearAndMonth(year, month);
    setState(() {
      this.data.clear();
      this.data.addAll(list);
      getDataSimpleSeriesLegend(year, month);
    });
  }

  getDataSimpleSeriesLegend(int year, int month) async {
    List<OrdinalSales> list = await AccountsProvider()
        .findByYearAndMonth(year, month, type: Accounts.SR);
    setState(() {
      this.data_sr.clear();
      this.data_sr.addAll(list);
    });
  }

  getSZData(int year) async {
    List<LinearSales> list = await AccountsProvider().findBySZ(year);
    List<LinearSales> _list = List();
    for (int i = 0; i < list.length; i++) {
      LinearSales linearSales = LinearSales(i, list[i].sales);
      linearSales.type = list[i].type;
      _list.add(linearSales);
    }

    setState(() {
      this.data_sz.clear();
      this.data_sz.addAll(_list);
    });
  }
}
